//
//  RadioViewController.m
//  A_Project_DLS151129_SQ
//
//  Created by 151129-49宋强 on 16/1/21.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "RadioViewController.h"
#import "RadioDetailViewController.h"

@interface RadioViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, assign)NSInteger start;

@end

@implementation RadioViewController
- (void)dealloc
{
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"电台";
    self.view.backgroundColor = [UIColor whiteColor];
    //创建tableView
    [self creatTableView];
    //数据处理
    [self dataHandle];
    self.start = 0;
    
    
}
- (void)dataHandle
{
    NSString *urlStr = @"http://api2.pianke.me/ting/radio";
    NSDictionary *dic = @{ @"start":@(self.start),@"client": @2, @"limit":@9};
    [PWNetWorking POSTWithURL:urlStr body:dic completion:^(id result) {
        NSArray *arr = result[@"data"][@"alllist"];
        self.dataArr = [Model modelHanderWithArray:arr];
        [self.tableView reloadData];
    }];
}
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    [_tableView release];
    //注册重用池
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.dataArr = nil;
        self.start = 0;
        [self dataHandle];
        //停止刷新
        [self.tableView.mj_header endRefreshing];
    }];
    self.tableView.mj_header = header;
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        //加载数据
        [self loadingData];
        //停止加载
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
 }
- (void)loadingData
{
    self.start += 9;
    NSString *urlStr = @"http://api2.pianke.me/ting/radio_list";
    NSDictionary *dic = @{@"start":@(self.start),@"client": @2, @"limit":@9};
    [PWNetWorking POSTWithURL:urlStr body:dic completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        NSArray *addArr = [Model modelHanderWithArray:arr];
        for (Model *m in addArr) {
            [self.dataArr addObject:m];
        }
        [self.tableView reloadData];
    }];

}

//区数
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return 1;
}
//区名
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"全部电台.All stations";
}
//cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
//cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
    Model *m = _dataArr[indexPath.row];
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:m.coverimg]placeholderImage:[UIImage imageNamed:@"coffe.png"]];
    cell.textLabel.text = m.title;
    cell.detailTextLabel.text = m.desc;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model *m = _dataArr[indexPath.row];
    RadioDetailViewController *radioDetailVC = [[RadioDetailViewController alloc] init];
    radioDetailVC.detailModel = m;
    [self.navigationController pushViewController:radioDetailVC animated:YES];
    [radioDetailVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
